package example4;

import org.junit.Test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataInputOutputTest {
    @Test
    public void write() {

        DataOutputStream dataOutputStream = null;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("3.txt");
            dataOutputStream = new DataOutputStream(fileOutputStream);
            dataOutputStream.writeInt(10);
            dataOutputStream.writeInt(-10);
            dataOutputStream.writeDouble(3.1415926);
            dataOutputStream.writeUTF("厉害了我的国");//utf8
            dataOutputStream.writeChars("厉害个屁");//utf16be
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Test
    public void read() {
        DataInputStream dataInputStream = null;
        try {
            FileInputStream fileInputStream = new FileInputStream("3.txt");
            dataInputStream = new DataInputStream(fileInputStream);

            System.out.println(dataInputStream.readInt());
            System.out.println(dataInputStream.readInt());

            System.out.println(dataInputStream.readDouble());

            System.out.println(dataInputStream.readUTF());

            System.out.println(dataInputStream.readChar());
            System.out.println(dataInputStream.readChar());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
